package org.project.controller2;



import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.project.pojo.Getfirstinfo;
import org.project.pojo.State;
import org.project.pojo.WxUser;
import org.project.service.user.WxUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import net.sf.json.JSONObject;
import wxpay.app.util.ConstantUtil;

import wxpay.web.utils.CommonUtil;

@Controller
@RequestMapping("/wxcontroller")
public class Wxcontroller {
	
	@Resource
	private    WxUserService  wxUserService;
	
	
	@RequestMapping("/getFirstInfo")
	@ResponseBody
	public Getfirstinfo getFirstInfo(@Param("code")String code) {
		System.out.println(code);
		String open_id = getOpenId(code);
		
		Getfirstinfo getfirstinfo=new Getfirstinfo();
		getfirstinfo.setOpen_id(open_id);
		getfirstinfo.setMessage("成功");
		getfirstinfo.setIsSuccessful(0);
		return getfirstinfo;
	}
	
	
	
	
	
	public static String getOpenId(String code){	
		 String Openid = "";
		String grant_type = "authorization_code";
		String weixin_url ="https://api.weixin.qq.com/sns/jscode2session";	
		String params =  "appid=" + ConstantUtil.APP_ID + "&secret=" + ConstantUtil.APP_SECRET + "&js_code=" + code + "&grant_type=" + grant_type;
		//String url  = ConstantUtil.TOKEN_URL+"?appid="
		//				+ConstantUtil.APP_ID+"&secret="+ConstantUtil.APP_SECRET+"&js_code=" + code + "&grant_type=authorization_code";
		//url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxb7af2cd287be1959&secret=4da52cc8b59554f8d3e8d2f13fabfe1a&js_code=061TzQGi1eI42x0gL6Hi1lXwGi1TzQGt&grant_type=authorization_code";
		String result = CommonUtil.httpsRequest(weixin_url, "POST", params);
		//String data = Utils.get(weixin_url, params);
		 
		//Map<String, String> map = new Hashtable<String, String>();
		try {
			//解析相应内容（转换成json对象）
	       JSONObject  json =JSONObject.fromObject(result); 
	        
	        //用户的唯一标识（openid）
	        Openid =String.valueOf(json.get("openid"));
	        
			//map = XMLUtil.doXMLParse(result);
			//System.out.println(map.toString());
		} catch (Exception e) {
			
		}
		//String openid = (String) map.get("openid");
		return Openid;
		}
	 	
	
	
	
	
	
	
	@RequestMapping("/insertwxuser")
	@ResponseBody
	public State insertwxuser(){
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String username= req.getParameter("username");
		String hphotourl=req.getParameter("hphotourl");
		String openid=req.getParameter("openid");
		WxUser wxuser=new WxUser();
		wxuser.setUsername(username);
		wxuser.setHphotourl(hphotourl);
		wxuser.setOpenid(openid);
		
		State state=new State();
		state.setState(0);//没有这个用户，且没有插入成功
		Boolean b=selectwxuser(wxuser.getOpenid());
		if(b==false){
	//		UUID uuid  =  UUID.randomUUID(); 
			String userid = UUID.randomUUID().toString();
			wxuser.setUserid(userid);
			wxUserService.insertwxuser(wxuser);
			state.setState(1);//插入成功
		}else{
			state.setState(2);//有这个用户，无需注册
		}
		return state;
		
	}
	
	
	
	public   boolean selectwxuser(String openid){
		Boolean b=false;
		WxUser wxuser=wxUserService.selectwxuser(openid);
		if(null!=wxuser){
			b=true;
		}
		return b;
	}

}
